package cn.com.infohold.smartcity.recceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

import org.greenrobot.eventbus.EventBus;

import cn.com.infohold.smartcity.bean.NetState;
import library.utils.Logger;


public class ReceiverNet extends BroadcastReceiver {

    private static Boolean lastConnectionState;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)) {

        } else if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {

        } else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (info.getState().equals(NetworkInfo.State.SUSPENDED)) {
            } else if (info.getState().equals(NetworkInfo.State.CONNECTING)) {
            } else if (info.getState().equals(NetworkInfo.State.CONNECTED)) {
            } else if (info.getState().equals(NetworkInfo.State.DISCONNECTING)) {
            } else if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {
            } else if (info.getState().equals(NetworkInfo.State.UNKNOWN)) {
            }
        } else if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
            int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
            switch (wifistate) {
                case WifiManager.WIFI_STATE_DISABLING:
                    break;
                case WifiManager.WIFI_STATE_DISABLED:
                    break;
                case WifiManager.WIFI_STATE_ENABLING:
                    break;
                case WifiManager.WIFI_STATE_ENABLED:
                    break;
                case WifiManager.WIFI_STATE_UNKNOWN:
                    break;
                default:
                    break;
            }
        } else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (info != null) {
                //printLog(info.getTypeName() + " : " + info.getState().name().toLowerCase());
                if (NetworkInfo.State.CONNECTED == info.getState()) {
                    onNetStateChanged(true);
                } else if (NetworkInfo.State.DISCONNECTED == info.getState()) {
                    onNetStateChanged(false);
                }
            }

        }
    }

    public void printLog(Object obj) {
        Logger.debug(obj);
    }

    protected synchronized void onNetStateChanged(boolean connected) {
        if (lastConnectionState == null || lastConnectionState != connected) {
            lastConnectionState = connected;
            printLog(connected ? "网络已连接" : "网络已断开");
            EventBus.getDefault().post(new NetState(connected));
        }
    }

}